package ua.kpi.flats.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import ua.kpi.flats.util.JsonDateSerializer;

@Entity
@JsonAutoDetect
@Table(name = "BUY_APPLICATION")
public class BuyApplication implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "APPLICATIONID")
	private long id;
	
	@Column(name = "CREATE_DATE")
	private Date createDate;
		
	@DecimalMin(value = "1")
	@DecimalMax(value = "999")
	@Column(name = "FLOOR")
	private Integer floor;
	
	@Min(value = 1)
	@Max(value = 9)
	@Column(name = "ROOMS")
	private int rooms;
	
	@DecimalMin(value = "1")
	@DecimalMax(value = "99999999.99")
	@Column(name = "PRICE_FROM")
	private BigDecimal priceFrom;
	
	@NotNull
	@DecimalMax(value = "99999999.99")
	@DecimalMin(value = "1")
	@Column(name = "PRICE_TO")
	private BigDecimal priceTo;
	
	@NotNull
	@ManyToOne(cascade = CascadeType.MERGE)
	@JoinColumn(name = "DISTRICTID")
	private District district;
	
	@NotNull
	@ManyToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "CLIENTID")
	private Client client;
	
	public BuyApplication() {
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	@JsonSerialize(using=JsonDateSerializer.class)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Integer getFloor() {
		return floor;
	}

	public void setFloor(Integer floor) {
		this.floor = floor;
	}

	public int getRooms() {
		return rooms;
	}

	public void setRooms(int rooms) {
		this.rooms = rooms;
	}

	public BigDecimal getPriceFrom() {
		return priceFrom;
	}

	public void setPriceFrom(BigDecimal priceFrom) {		
		this.priceFrom = priceFrom;		
	}

	public BigDecimal getPriceTo() {
		return priceTo;
	}

	public void setPriceTo(BigDecimal priceTo) {
		this.priceTo = priceTo;
	}

	public District getDistrict() {
		return district;
	}

	public void setDistrict(District district) {
		this.district = district;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((client == null) ? 0 : client.hashCode());
		result = prime * result
				+ ((createDate == null) ? 0 : createDate.hashCode());
		result = prime * result
				+ ((district == null) ? 0 : district.hashCode());
		result = prime * result + floor;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result
				+ ((priceFrom == null) ? 0 : priceFrom.hashCode());
		result = prime * result + ((priceTo == null) ? 0 : priceTo.hashCode());
		result = prime * result + rooms;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BuyApplication other = (BuyApplication) obj;
		if (client == null) {
			if (other.client != null)
				return false;
		} else if (!client.equals(other.client))
			return false;
		if (createDate == null) {
			if (other.createDate != null)
				return false;
		} else if (!createDate.equals(other.createDate))
			return false;
		if (district == null) {
			if (other.district != null)
				return false;
		} else if (!district.equals(other.district))
			return false;
		if (floor != other.floor)
			return false;
		if (id != other.id)
			return false;
		if (priceFrom == null) {
			if (other.priceFrom != null)
				return false;
		} else if (!priceFrom.equals(other.priceFrom))
			return false;
		if (priceTo == null) {
			if (other.priceTo != null)
				return false;
		} else if (!priceTo.equals(other.priceTo))
			return false;
		if (rooms != other.rooms)
			return false;
		return true;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Rooms: ").append(rooms);
		builder.append(",PriceFrom: ").append(priceFrom);
		builder.append(",PriceTo: ").append(priceTo);
		builder.append(",Floor: ").append(floor);
		builder.append(",ClientId: ").append(client.getId());
		builder.append(",DistrictId: ").append(district.getId());
		return builder.toString();
	}
}
